package com.sqrj.phoneorder.Controller;

import com.sqrj.phoneorder.model.SendChart;
import com.sqrj.phoneorder.model.WxOrderUnit;
import com.sqrj.phoneorder.service.SendTruckOUTService;
import com.sqrj.phoneorder.service.WxOrderUnitService;
import com.sqrj.phoneorder.service.WxHntRwdService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
public class SendChartController {

    @Resource
    private SendTruckOUTService sendTruckOUTService;

    @Resource
    private WxHntRwdService wxHntRwdService;

    @Resource
    private WxOrderUnitService wxOrderUnitService;

    //查询chart图表数据
    @PostMapping("/querySendChart")
    public Map<String, Object> querySendChart(@RequestBody Map<String, Object> map) {
        //用户id，判断为哪一个用户
        String Wxid = (String) map.get("openId");
        List<WxOrderUnit> wxUnitList = wxOrderUnitService.queryWxUnitById(Wxid);
        Map<String, Object> modelMap = new HashMap<String, Object>();
        //初始化数据
        Double[] data1 = new Double[]{0.0, 0.0, 0.0, 0.0};
        Double[] data2 = new Double[]{0.0, 0.0, 0.0, 0.0};
        if (wxUnitList.size() > 0) {
            //默认获取用户最新添加的一个单位
            String UnitName = wxUnitList.get(0).getUnitName();
            //计划量
            List<SendChart> SCPList = wxHntRwdService.querySendChart(UnitName);
            //实际量
            List<SendChart> SCSList = sendTruckOUTService.querySendChart(UnitName);
            //初始化日期格式
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            //获取当前时间，将最近日期转换为字符串
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            String date3 = sdf.format(cal.getTime());
            cal.add(Calendar.DATE, 1);
            String date4 = sdf.format(cal.getTime());
            cal.add(Calendar.DATE, -2);
            String date2 = sdf.format(cal.getTime());
            cal.add(Calendar.DATE, -1);
            String date1 = sdf.format(cal.getTime());
            // 判断对应日期进行赋值当天计划量数据，可重构
            for (int i = 0; i < SCPList.size(); i++) {
                if (SCPList.get(i).getSendDate().equals(date1)) {
                    data1[0] = SCPList.get(i).getOrderQuantity();
                } else if (SCPList.get(i).getSendDate().equals(date2)) {
                    data1[1] = SCPList.get(i).getOrderQuantity();
                } else if (SCPList.get(i).getSendDate().equals(date3)) {
                    data1[2] = SCPList.get(i).getOrderQuantity();
                } else if (SCPList.get(i).getSendDate().equals(date4)) {
                    data1[3] = SCPList.get(i).getOrderQuantity();
                }
            }

            //判断对应日期进行赋值当天实际量数据
            System.out.println("data2" + data1[2]);
            for (int i = 0; i < SCSList.size(); i++) {
                if (SCSList.get(i).getSendDate().equals(date1)) {
                    data2[0] = SCSList.get(i).getOrderQuantity();
                } else if (SCSList.get(i).getSendDate().equals(date2)) {
                    data2[1] = SCSList.get(i).getOrderQuantity();
                } else if (SCSList.get(i).getSendDate().equals(date3)) {
                    data2[2] = SCSList.get(i).getOrderQuantity();
                } else if (SCSList.get(i).getSendDate().equals(date4)) {
                    data2[3] = SCSList.get(i).getOrderQuantity();
                }
            }

            modelMap.put("SCPList", SCPList);
        }
        modelMap.put("data1", data1);
        modelMap.put("data2", data2);
        return modelMap;
    }
}

